package com.limingru.online_class.controller;

import com.limingru.online_class.common.Result;
import com.limingru.online_class.common.ResultGenerator;
import com.limingru.online_class.common.ServiceResultEnum;
import com.limingru.online_class.controller.param.RemoveOrderParam;
import com.limingru.online_class.controller.param.SaveOrderParam;
import com.limingru.online_class.domain.VideoOrder;
import com.limingru.online_class.service.VideoOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/v1/pri/order")
public class VideoOrderController {

    @Autowired
    VideoOrderService videoOrderService;

    @PostMapping("save")
    public Result saveOrder(@RequestBody SaveOrderParam saveOrderParam, HttpServletRequest request) {
        Integer videoId = saveOrderParam.getVideoId();
        Integer userId = (Integer) request.getAttribute("userId");

        int saveResult = videoOrderService.save(videoId, userId);
        return saveResult > 0
                ? ResultGenerator.generateSuccessResult()
                : ResultGenerator.generateFailResult();
    }

    @GetMapping("list")
    public Result listOrder(HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        List<VideoOrder> result = videoOrderService.listOrders(userId);
        return ResultGenerator.generateSuccessResult(result);
    }

    @PostMapping("remove")
    public Result removeOrder(@RequestBody RemoveOrderParam removeOrderParam, HttpServletRequest request) {
        Integer userId = (Integer) request.getAttribute("userId");
        Integer orderId = removeOrderParam.getOrderId();
        String result = videoOrderService.removeOrder(orderId, userId);
        if (ServiceResultEnum.SUCCESS.getText().equals(result)) {
            return ResultGenerator.generateSuccessResult();
        }
        return ResultGenerator.generateFailResult(result);
    }
}
